
exports.GAFileDownloader = Core.Class.subclass({
    classname: 'GAFileDownloader',
    initialize: function(filename, method, url, headers, delegate, requestTag) {
        this._filename = filename;
        this._method = method;
        this._url = url;
        this._headers = headers;
        this._delegate = delegate;
        this.requestTag = requestTag;
        this._started = false;
    },
    startDownloading: function() {  
        this._downloader = new Network.DownloadFile();
        this._downloader.start(this._filename,this._method, this._url, this._headers, this._doneCb.bind(this));
        this._started = true;
    },
    _doneCb: function(statusCode,fileSignature) {
    	this._started = false;
    	if(this._delegate != null){
	        if(statusCode == httpStatus.HTTP_STATUS_SUCCESS_OK){
	        	this._delegate.onDownloadSuccess(this, fileSignature);
	        }
	        else {
	        	this._delegate.onDownloadFailure(this, statusCode);
	        }
        }
    },
    invalidate: function (){
    	if(this._downloader != null){
    		if (this._started == true) {
    			this._downloader.abort();
    		}
    		this._downloader = null;
    	}
    },
    destroy: function() {
        this._filename = null;
        this._method = null;
        this.invalidate();
        this._url = null;
        this._headers = null;
        this._delegate = null;
        this._filename = null;
        this._method = null;
        this._url = null;
        this._headers = null;
        this._delegate = null;
        this.requestTag = null;
    }
});
